<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 角色管理
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-02-25 13:43:37 +0800 (二, 2014-02-25) $
 * @version : $Id: role.php 31 2014-02-25 05:43:37Z spike $
 * @rev     : $Revision: 31 $
 */
class Role extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Rbac_model');
            $list = $this->Rbac_model->browse_role($params, $params['page'], $params['size']);

            // 绑定成员数
            $this->Rbac_model->wrap_role_member_count($list['data']);

            // 绑定角色信息
            $this->Rbac_model->wrap_role_info($list['data']);
            
            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 统计
        $role_status = $this->config->item('role_status');

        $this->load->dao('Role_dao');
        $total = $this->Role_dao->count(array(
            'is_delete' => UNDELETED
        ));

        // 筛选
        $pagesize     = $this->config->item('pagesize');

        $data = array(
            'default'     => $params ? $params : array(),
            'total'       => number_format($total),
            'list'        => $list,
            'pagination'  => $pagination,
            'pagesize'    => $pagesize,
            'role_status' => $role_status,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('role/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [改变角色状态]
     */
    public function change_status($id = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Rbac_model');
        if( FALSE === $this->Rbac_model->change_role_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Rbac_model->get_error());
            }
            else
            {
                show_error($this->Rbac_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("改变角色状态成功！", array(
                    '返回角色管理' => DOMAIN."role/browse",
                ));
            }
        }
    }

    /**
     * [新增角色]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_role', $params);

            $this->load->model('Rbac_model');

            if( FALSE === $admin_id = $this->Rbac_model->add_role($params) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }
            temp('add_role', NULL);

            show_message(
                "新增角色成功！", 
                array(),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->dao('Role_dao');
            $role = $this->Role_dao->get(
                array(
                    'is_delete' => UNDELETED,
                    'status'    => ROLE_STATUS_ON,
                ),
                array('role_id','name')
            );

            // 获取所有资源类型
            $role_resource_group = $this->config->item('role_resource_group');

            // 获取所有资源列表
            $this->load->dao('Role_resource_dao');
            $role_resource = $this->Role_resource_dao->get(
                array('is_delete' => UNDELETED),
                array('resource_id','name')
            );

            // 获取所有用户
            $this->load->dao('Admin_dao');
            $admin = $this->Admin_dao->get(
                array('is_delete' => UNDELETED),
                array('admin_id','nickname')
            );

            $page_style = $this->config->item('page_style');
            $data = array(
                'temp'                => temp('add_role'),
                'role_resource_group' => $role_resource_group,
                'role_resource'       => $role_resource,
                'admin'               => $admin,
                'page_style'          => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('role/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑角色]
     */
    public function edit_role($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Rbac_model');

            if( FALSE === $this->Rbac_model->edit_role($id, $params) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑角色成功！", 
                array('查看编辑的会员' => DOMAIN."role/edit_role/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Rbac_model');
            if( ! $role = $this->Rbac_model->get_role($id, FALSE))
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            $page_style    = $this->config->item('page_style');
            $data = array(
                'role'       => $role,
                'page_style' => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('role/edit_role.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑权限]
     */
    public function edit_priv($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Rbac_model');
            
            if( FALSE === $this->Rbac_model->edit_role_priv($id, $params['privs']) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑角色权限成功！", 
                array('查看角色权限' => DOMAIN."role/edit_priv/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Rbac_model');
            if( FALSE === $role = $this->Rbac_model->get_role($id, TRUE, TRUE))
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            // 获取所有资源类型
            $role_resource_group = $this->config->item('role_resource_group');

            // 整理角色资源
            if( is_array($role['privs']) )
            {
                $temp = array();
                foreach ($role['privs'] as $rp) 
                {
                    foreach ($role_resource_group as $rg => $rgv) 
                    {
                        $col = strtolower($rg)."_priv";
                        if( $rp[$col] == ROLE_PRIV_ON )
                            $temp[] = "{$rp['resource_id']}-{$rg}";
                    }
                }
                $role['privs'] = $temp;
            }

            // 获取所有资源列表
            $this->load->dao('Role_resource_dao');
            $role_resource = $this->Role_resource_dao->get(
                array('is_delete' => UNDELETED),
                array('resource_id','name')
            );

            $page_style = $this->config->item('page_style');
            $data = array(
                'role'                => $role,
                'role_resource_group' => $role_resource_group,
                'role_resource'       => $role_resource,
                'page_style'          => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('role/edit_priv.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [删除权限]
     */
    public function del_priv($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $this->load->model('Rbac_model');
            
            if( FALSE === $this->Rbac_model->del_role_priv($id) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }
            
            show_message(
                "删除角色权限成功！", 
                array('查看角色权限' => DOMAIN."role/edit_priv/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
    }

    /**
     * [编辑成员]
     */
    public function edit_member($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Rbac_model');

            if( FALSE === $admin_id = $this->Rbac_model->edit_role_member($id, $params['admins']) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑成员成功！", 
                array('查看角色成员' => DOMAIN."role/edit_member/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Rbac_model');
            if( FALSE === $role = $this->Rbac_model->get_role($id, TRUE, FALSE, TRUE) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            // 获取所有用户
            $this->load->dao('Admin_dao');
            $admin = $this->Admin_dao->get(
                array('is_delete' => UNDELETED),
                array('admin_id','nickname')
            );
            $page_style = $this->config->item('page_style');
            $data = array(
                'admin'      => $admin,
                'role'       => $role,
                'page_style' => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('role/edit_member.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [删除成员]
     */
    public function del_member($id = NULL)
    {
        if($this->input->is_post_request())
        {

            $this->load->model('Rbac_model');
            
            if( FALSE === $this->Rbac_model->del_role_member($id) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }
            
            show_message(
                "删除角色成员成功！", 
                array('查看角色成员' => DOMAIN."role/edit_member/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
    }
}
/* End of file */